<template>
  <a-modal
    :visible='clientVisible'
    title="多边形围栏配置(设置给终端黑匣子设备，由黑匣子计算和告警，可以设置20个)"
    width="100%"
    :footer="null"
    :mask-closable='false'
    :destroy-on-close='true'
    :style="{ top: '0px' }"
    @update:visible='updateClientVisible'
  >
    <div class="ele-body">

      <!-- 表格 -->
      <div>
        <a-card :bordered="false">
          <ele-pro-table
            ref="table"
            row-key="vgpcId"
            :datasource="datasource"
            :columns="columns"
            :where="where"
            :scroll="{ x: 'max-content' }"
          >
            <!-- table上边工具栏 -->
            <template #toolbar>
              <a-space>
                <a-button type="primary" @click="openEdit()">
                  <template #icon>
                    <plus-outlined />
                  </template>
                  <span>新建</span>
                </a-button>
              </a-space>
            </template>

            <template #bodyCell="{ column, record }">
              <!-- table操作栏按钮 -->
              <template v-if="column.key === 'action'">
                <a-space>
                  <a @click="openEdit(record)">修改</a>
                  <a-divider type="vertical" />
                  <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                    <a class="ele-text-danger">删除</a>
                  </a-popconfirm>
                </a-space>
              </template>

            </template>
          </ele-pro-table>
        </a-card>
      </div>

      <!-- 编辑弹窗 -->
      <vehicle-manager-geofence-list-client-manager-edit v-if="showEdit" v-model:editVisible="showEdit" :vehicleData="vehicleData" :data="editCurrent" @done="reload" />
    </div>
  </a-modal>
</template>

<script>
import { message } from 'ant-design-vue';
import {VehicleGeofencePolygonClientApi} from "@/api/property/vechile/VehicleGeofencePolygonClientApi";
import VehicleManagerGeofenceListClientManagerEdit from './fenceclientmanager/polygon-geofence-edit.vue'

export default {
  name: 'VehicleManagerGeofenceListPolygonClient',
  components: {
    VehicleManagerGeofenceListClientManagerEdit
  },
  props: {
    // 弹窗是否打开
    clientVisible: Boolean,
    data: Object
  },
  emits: ['update:clientVisible'],
  created() {
    console.log("xxxx");
    console.log(this.data);
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '围栏ID',
          dataIndex: 'geoId'
        },
        {
          title: '围栏名称',
          dataIndex: 'geofenceName'
        },
        {
          title: '操作',
          key: 'action',
          width: 240,
          align: 'center',
          fixed: 'right'
        }
      ],
      // 表格搜索条件
      where: {
        "vehicleId" : this.data.vehicleId
      },
      // 当前编辑数据
      editCurrent: null,
      vehicleData:null,
      // 是否显示编辑弹窗
      loadTimer:null, //定义一个刷新的timer
      showEdit : false
    };
  },
  methods: {
    loadPage() {
      this.$refs.table.reload({ });
    },
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2023/10/03 23:15
     */
    reload() {
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2023/10/03 23:15
     */
    reset() {
      this.where.vehicleId =  this.data.vehicleId;
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2023/10/03 23:15
     */
    async remove(row) {
      const result = await VehicleGeofencePolygonClientApi.delete({ vgpcId: row.vgpcId });
      message.success(result.message);
      this.reload();
    },

    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return VehicleGeofencePolygonClientApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    },
    updateClientVisible(value) {
      this.$emit('update:clientVisible', value);
    },
    openEdit(row) {
      if (this.data.recordId == undefined || this.data.recordId == null || this.data.recordId == 0) {
        message.error("车辆还没办法绑定黑匣子");
      } else {
        this.editCurrent = row;
        this.vehicleData = this.data;
        this.showEdit = true;
      }
    },

  }
};
</script>
